package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAOCondition;
import br.ufrj.ppgi.greco.model.dao.DAOFormulaParameter;
import br.ufrj.ppgi.greco.model.dao.DAOProperty;
import br.ufrj.ppgi.greco.model.dao.DAOResource;
import br.ufrj.ppgi.greco.model.dao.DAOVirtualDataProperty;
import br.ufrj.ppgi.greco.model.entity.EntityCondition;
import br.ufrj.ppgi.greco.model.entity.EntityFormulaParameter;
import br.ufrj.ppgi.greco.model.entity.EntityResource;
import br.ufrj.ppgi.greco.model.entity.EntityVirtualDataProperty;

@Resource
public class VirtualDataPropertyController
{

	private DAOResource dr;
	private DAOProperty dp;
	private DAOVirtualDataProperty dv;
	private DAOFormulaParameter dfp;
	private DAOCondition dc;
	private Result result;

	public VirtualDataPropertyController(DAOProperty dp, DAOResource dr, DAOVirtualDataProperty dv, Result result, DAOCondition dc, DAOFormulaParameter dfp)
	{
		this.dr = dr;
		this.dv = dv;
		this.dfp = dfp;
		this.dc = dc;
		this.dp = dp;
		this.result = result;
	}

	@Get
	@Path("/config/resources/{id}/virtualDataProperties/new")
	public void newVirtualDataProperty(int id)
	{
		this.result.include("id", id);
		this.result.include("properties",this.dp.getAllPropertiesJson());
	}

	@Post
	@Path("/config/resources/{id}/virtualDataProperties/create")
	public void createVirtualDataProperty(EntityVirtualDataProperty vdp , String url , int id)
	{
		EntityResource er = this.dr.getByID(id);
		vdp.setResource(er);
		this.dv.insert(vdp);
		
		er.getVirtualDataProperties().add(vdp);
		this.dr.update(er);

		vdp.setHasConditional(false);

		
		if (vdp.getFormulaParameters() != null)
		{
			for (EntityFormulaParameter ef : vdp.getFormulaParameters())
			{	
				ef.setVirtualDataProperty(vdp);
				this.dfp.insert(ef);
			}
		}
		
		if (vdp.getConditions() != null)
		{
			vdp.setHasConditional(true);
			for (EntityCondition ec : vdp.getConditions())
			{				
				ec.setVirtualDataProperty(vdp);		
				this.dc.insert(ec);	
			}
		}
		
		this.dv.update(vdp);
		
		this.result.redirectTo(ResourceController.class).listResource(id);
	}

	@Get
	@Path("/config/resources/{id}/virtualDataProperties/{idVirtualDP}")
	public void listVirtualDataProperty(int id , int idVirtualDP)
	{
		this.result.include("virtual", this.dv.getByID(idVirtualDP));
		this.result.include("id", id);
	}

	

	@Get
	@Path("/config/resources/{resourceId}/virtualDataProperties/{idVirtualDP}/delete")
	public void deleteVirtualDataProperty(int resourceId , int idVirtualDP)
	{
		EntityVirtualDataProperty evp = this.dv.getByID(idVirtualDP);
		
		if(evp.getConditions() != null)
		{
			for(EntityCondition ec : evp.getConditions())
				this.dc.deleteByID(ec.getId());
			
			evp.setConditions(null);
		}
		
		if(evp.getFormulaParameters() != null)
		{
			for(EntityFormulaParameter efp: evp.getFormulaParameters())
			{					
				this.dfp.deleteByID(efp.getId());
			}	
			evp.setFormulaParameters(null);
		}

		this.dv.update(evp);
		
		this.dv.deleteByID(idVirtualDP);
		this.result.redirectTo(ResourceController.class).listResource(resourceId);
	}

}
